<%-- 
    Document   : question-all-category
    Created on : Feb 9, 2012, 8:12:43 AM
    Author     : PhucAn
--%>

<%@page import="util.SharingUtil"%>
<%@page import="dto.QuestionCategory"%>
<%@page import="java.util.ArrayList"%>
<%@page import="dao.QuestionCategoryDAO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    String message = "";
    QuestionCategoryDAO daoQuestionCate = new QuestionCategoryDAO();
    if (request.getParameter("add") != null) {


        QuestionCategory cate = new QuestionCategory();
        cate.setName(request.getParameter("name"));
        cate.setDescription(request.getParameter("description"));
        if (daoQuestionCate.create(cate) > 0) {
            message = "New category is created";
        } else {
            message = "Error occured. Category cannot be created";
        }
        session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, message);
    } else {
%>
<%
        if (request.getParameter("remove") != null) {

            int id = Integer.parseInt(request.getParameter("selected-item-id"));
            if (daoQuestionCate.delete(id) > 0) {
                message = "Category is deleted";
            } else {
                message = "Error occured. Category cannot be deleted";
            }
            session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, message);
        }
    }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Question categories</title>       

        <script>
            function setValue(identifier){
                //set value for hidden field. This field hold the 'id' value of selected item                
                document.getElementById("selected-item-id").value=identifier;               
            }
            
        </script>
    </head>
    <body>      
 
            <form method="post" action="#">
                <table class="data-table">
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                        <th></th>
                        <th></th>
                    </tr>
                    <%
                        ArrayList<QuestionCategory> list = daoQuestionCate.getAllCategories();

                        for (QuestionCategory cate : list) {
                    %>
                    <tr>
                        <td><%=cate.getName()%></td>
                        <td><%=cate.getDescription()%></td>
                        <td><a href="?pcontent=add-question&cateid=<%=cate.getId() %>">Add question</a></td>
                        <td><input type="button" name="set-value" value="Delete" onclick='showConfirm();setValue(<%=cate.getId()%>);'/></td>
                    </tr>
                    <%}%>   

                </table>              
                <input type="hidden" id="selected-item-id" name="selected-item-id" value=""/>
                <div class="confirm-box">
                    <div>Are you sure want to continue?</div>
                    <input type="submit" name="remove" onclick="hideConfirm();" value="Delete"/>
                    <input type="button" value="Cancel"/>
                </div>
            </form>
            <form method="post" action="#">
                <table class="form-table">
                    <tr>
                        <td>Category name:</td>
                        <td><input type="text" name="name"/></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Description: </td>
                        <td><input type="text" name="description"/></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td colspan="2"><input type="submit" name="add" value="Create"/></td>
                    </tr>
                </table>                
            </form>
      
    </body>
</html>

